package com.ruoyi.business.question;

import com.ruoyi.business.mapper.ObjectiveAnswerMapper;
import com.ruoyi.business.mapper.QuestionMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;

import java.util.List;

/**
 * @author 最爱喝兽奶
 * @create 2022/8/9
 **/
public abstract class AbstractQuestionHandle implements QuestionHandle {


    protected QuestionMapper questionMapper;

    protected ObjectiveAnswerMapper objectiveAnswerMapper;

    protected String separator = ";";


    public AbstractQuestionHandle() {
        this.questionMapper = SpringUtils.getBean(QuestionMapper.class);
        this.objectiveAnswerMapper = SpringUtils.getBean(ObjectiveAnswerMapper.class);
    }

    protected void validateRandQuestionParams(String courseId, int limit) {
        if (StringUtils.isBlank(courseId)) {
            throw new RuntimeException("不能传入空的课程id");
        }
    }

    @Override
    public List<String> randQueryQuestion(String courseId, int limit) {
        validateRandQuestionParams(courseId, limit);
        List<String> questions = randQueryQuestionByType(courseId, limit);
        if (questions.size() < limit) {
            throw new RuntimeException("题目数量不够，生成试卷失败");
        }
        return questions;
    }

    @Override
    public void deleteQuestion(String id) {
        questionMapper.deleteById(id);
    }

    @Override
    public Integer calcScore(String answer, String questionId, Integer score) {
        return null;
    }

    /**
     * 根据类型随机查询几道题目
     * @param courseId 课程id
     * @param limit 查询的题目条数
     * @return
     */
    public abstract List<String> randQueryQuestionByType(String courseId, int limit);


}
